﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{
    /// <summary>
    /// A MainWindow is a window moveable with a CMenuBar as menu bar and a CWidget as content.
    /// </summary>
    [AttrTypeDependentSerializable(typeof(CMainWindowData))]
    public class CMainWindow : CWidget
    {
        #region fields

        private CMenuBar m_menubar = null;
        private CWidget m_content = null;

        #endregion

        #region properties

        /// <summary>
        /// Gets/Sets the title displayed on the menubar
        /// </summary>
        public string Title { get { return m_menubar.Title.Text; } set { m_menubar.Title.Text = value; } }

        /// <summary>
        /// If the value is between 0 and 1, the size is a percentage of the size of the parent
        /// (if the parent is null, the value is a percentage of the window).
        /// Else, the value is absolute.
        /// </summary>
        public override Vector2 Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;
                Actualize();
            }
        }

        /// <summary>
        /// If the value is between 0 and 1, the position is a percentage of the size of the parent
        /// (if the parent is null, the value is a percentage of the window).
        /// The value is relative to its parent.
        /// </summary>
        public override Vector2 Position
        {
            get
            {
                return base.Position;
            }
            set
            {
                base.Position = value;
                Actualize();
            }
        }

        /// <summary>
        /// Gets/Sets the rectangle where the widget will be draw.
        /// This value is automatically set when the position and/or the size are set.
        /// If you plan to use a different rectangle than the one automatically created,
        /// we recommand you to use the UseDrawableRect and DrawableRect properties.
        /// </summary>
        /// <seealso cref="UseDrawableRect"/>
        /// <seealso cref="DrawableRect"/>
        public override Rectangle Rect
        {
            get
            {
                return base.Rect;
            }
            set
            {
                base.Rect = value;
                Actualize();
            }
        }

        /// <summary>
        /// Gets/Sets the texture for the content background.
        /// </summary>
        public override CHudTexture Texture
        {
            get { return m_content.Texture; }
            set { if (m_content != null) m_content.Texture = value; }
        }

        /// <summary>
        /// Gets the children of the content (if this one isn't null)
        /// </summary>
        public override Dictionary<string, CWidget> Children
        {
            get
            {
                if (m_content == null)
                    return m_children;
                return m_content.Children;
            }
        }

        /// <summary>
        /// Gets/Sets the menubar of this window
        /// </summary>
        public CMenuBar Menubar
        {
            get { return m_menubar; }
            set
            {
                m_menubar = value;
                m_menubar.Size = new Vector2(1f, 0.1f);
                m_menubar.Position = new Vector2(0, 0);
            }
        }

        /// <summary>
        /// Gets/Sets the content widget of this window.
        /// </summary>
        public CWidget Content
        {
            get { return m_content; }
            set
            {
                m_content = value;
                m_content.Size = new Vector2(1f, 0.9f);
                m_content.Position = new Vector2(0, 0.1f);
            }
        }

        /*public override eventMethod OnClick { get { return m_content.OnClick; } set { m_content.OnClick = value; } }
        public override eventMethod OnRelease { get { return m_content.OnRelease; } set { m_content.OnRelease = value; } }
        public override eventMethod OnMouseOver { get { return m_content.OnMouseOverEnter; } set { m_content.OnMouseOverEnter = value; } }
        public override eventMethod OnMouseOverExit { get { return m_content.OnMouseOverExit; } set { m_content.OnMouseOverExit = value; } }*/

        /// <summary>
        /// Method called whenever the mouse is getting over the widget.
        /// </summary>
        public override bool onMouseOverEnter(CMouse mouse)
        {
            if (m_menubar.isMouseOver(mouse))
                return m_menubar.onMouseOverEnter(mouse);
            if (m_content.isMouseOver(mouse))
                return m_content.onMouseOverEnter(mouse);
            return true;
        }

        /// <summary>
        /// Method called whenever the mouse is getting out the widget.
        /// </summary>
        public override bool onMouseOverExit(CMouse mouse)
        {
            if (m_menubar.WasMouseOver)
                m_menubar.onMouseOverExit(mouse);
            if (m_content.WasMouseOver)
                m_content.onMouseOverExit(mouse);
            return true;
        }

        #endregion

        #region actualize/giveSubRect

        public override void Actualize()
        {
            if (m_parent != null)
                this.m_parent.giveSubRect(m_position, m_size, ref m_rect);
            m_content.Size = new Vector2(m_rect.Width, m_rect.Height - 15);
            m_content.Position = new Vector2(0, 15);
            m_content.Actualize();
            m_menubar.Size = new Vector2(m_rect.Width, 15);
            m_menubar.Actualize();
            m_content.Texture.Rectangle = m_content.Rect;
        }

        public override void giveSubRect(Vector2 pos, Vector2 size, ref Rectangle rectangle)
        {
            giveSubRect(pos, size, ref rectangle, false);
        }

        public override void giveSubRect(Vector2 pos, Vector2 size, ref Rectangle rectangle, bool force)
        {
            if (m_content != null && m_menubar != null && rectangle != m_content.Rect && rectangle != m_menubar.Rect)
                Content.giveSubRect(pos, size, ref rectangle);
            else
                base.giveSubRect(pos, size, ref rectangle);
        }


        #endregion

        #region ctor

        public CMainWindow(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            m_menubar = new CMenuBar(hud, this, name + "_menuBar");
            m_menubar.Position = new Vector2(0, 0);
            m_menubar.Size = new Vector2(m_rect.Width, 15);
            m_content = new CWidget(hud, this, name + "_content");
            m_content.Texture = HUD.Skinner.GetTexture("mainwindow");
            m_content.Size = new Vector2(m_rect.Width, m_rect.Height - 15);
            m_content.Position = new Vector2(0, 15);
            Size = new Vector2(400, 400);
        }

        #endregion

        #region events

        /// <summary>
        /// Method called whenever the mouse is released and is over this widget and was pressed over this widget
        /// </summary>
        public override bool onRelease(CMouse mouse)
        {
            bool ret = IsLeftClicked;
            if (m_activatedCWidget != null)
            {
                m_activatedCWidget.onRelease(mouse);
                if (!m_activatedCWidget.isMouseOver(mouse))
                    m_activatedCWidget.InvokeRelease(m_activatedCWidget);
                m_activatedCWidget = null;
            }
            else if (IsLeftClicked && isMouseOver(mouse))
            {
                InvokeRelease(this);
                IsLeftClicked = false;
            }
            return ret;
        }

        #endregion

        public override bool Draw(GameTime gameTime)
        {
            return base.Draw(gameTime);
        }

    }
}
